package com.bawei.module_shop.address;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.library_base.RouterPath;
import com.bawei.library_base.mvp.v.BaseActivity;
import com.bawei.library_base.room.address.Address;
import com.bawei.library_base.room.address.AddressDao;
import com.bawei.library_base.room.address.AddressDatabase;
import com.bawei.module_shop.R;
import com.bawei.module_shop.address.adapter.AddressAdapter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.jetbrains.annotations.NotNull;

import java.util.List;

@Route(path = RouterPath.AddressDiZhi.ADDRESSDIZHI)
public class AddressActivity extends BaseActivity {

    private Button mAddAddressBtn;
    private RecyclerView addressRv;
    private AddressAdapter addressAdapter;

    private int index=0;

    @Override
    public int beanLayout() {
        return R.layout.activity_address;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        mAddAddressBtn = (Button) findViewById(R.id.mAddAddressBtn);
        addressRv = (RecyclerView) findViewById(R.id.address_rv);
        addressRv.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
    }

    @Override
    public void initData() {



        //创建新地址
        newAddress();

        //显示添加过的地址
        showAddress();


    }


    @Subscribe
    public void evbus(String evenbus){
        if (evenbus.equals("addSuccess")){
            ToastUtils.showShort("111");
            showAddress();
//            addressAdapter.notifyDataSetChanged();
            //TODO("桥到这里")
        }
    }

    private void showAddress() {
        List<Address> allAddress = AddressDatabase.getInstance(this).getAddressDao().getAllAddress();
        addressAdapter = new AddressAdapter(allAddress);

        addressAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                if (view.getId()==R.id.address_delete){
                    addressAdapter.getData().remove(position);
                    addressAdapter.notifyItemRemoved(position);

                    AddressDatabase.getInstance(getApplicationContext()).getAddressDao().delete(allAddress.get(position));
                }



//                CheckBox checkBox = (CheckBox) addressAdapter.getViewByPosition(position, R.id.address_check);
                if (view.getId()==R.id.address_check){
                    index++;
                    if (index%2==1){
                        allAddress.get(position).setIstrue(false);
                        AddressDatabase.getInstance(getApplicationContext()).getAddressDao().Update(allAddress.get(position));
                        CheckBox checkBox = (CheckBox) addressAdapter.getViewByPosition(position, R.id.address_check);
                        checkBox.setChecked(false);
                        ToastUtils.showShort("点");
                    }else {
                        allAddress.get(position).setIstrue(true);
                        AddressDatabase.getInstance(getApplicationContext()).getAddressDao().Update(allAddress.get(position));
                        CheckBox checkBox = (CheckBox) addressAdapter.getViewByPosition(position, R.id.address_check);
                        checkBox.setChecked(true);
                        ToastUtils.showShort("点");
                    }


//                    checkBox.setChecked(true);
                }
            }
        });
        addressRv.setAdapter(addressAdapter);

    }

    private void newAddress() {
        mAddAddressBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build(RouterPath.NewAddress.NEWADDRESS)
                        .navigation();
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}